/**
 * 
 */
package com.org.javabody.Java_to_Oracle;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**

 * @author admin

 *

 */
public class connectOracleDemo {
	private static final String DRIVER="oracle.jdbc.driver.OracleDriver";
	private static final String URL = "jdbc:oracle:thin:@127.0.0.1:1521:oracle";
	private static final String USERID = "system";
	private static final String PASSWORD = "system";
	public static Connection createSQL() throws ClassNotFoundException, SQLException {
		Connection con=null;
		try {
			Class.forName(DRIVER);
			con = DriverManager.getConnection(URL,USERID, PASSWORD);
		}catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return con;
	}

	public static void print() throws ClassNotFoundException, SQLException {

		Connection con = createSQL();
		PreparedStatement pre = null;// 创建预编译语句对象，一般都是用这个而不用Statement

		ResultSet result = null;// 创建一个结果集对象

		try
		{

			String sql = "select * from AC01 where aac001=?";// 预编译语句，“？”代表参数

			pre = con.prepareStatement(sql);// 实例化预编译语句

			pre.setLong(1, 1);
			result = pre.executeQuery();// 执行查询，注意括号中不需要再加参数

			while (result.next())
				// 当结果集不为空时


				System.out.println("" + result.getLong("aac001") + " " 
						+ result.getString("aae135"));
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				// 逐一将上面的几个对象关闭，因为不关闭的话会影响性能、并且占用资源
				// 注意关闭的顺序，最后使用的最先关闭
				if (result != null)
					result.close();
				if (pre != null)
					pre.close();
				if (con != null)
					con.close();
				System.out.println("数据库连接已关闭！");
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}		
	}

	/**

	 * @param args

	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			print();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}